import obspython as obs
import requests
import sys

start_cookies_bls = {
  "buvid3": "ADB2785E-7985-D51E-DD66-7128D210647B58320infoc",
  "b_nut": "1734113758",
  "buvid4": "0F6C7BCD-A1D7-ED20-5733-4FBDAD17ED0358320-024121318-caFc0FJUezoTVcDfjrKgMQ%3D%3D",
  "_uuid": "10F410A2A4-5C5F-10389-BE71-75E1877D5E9561343infoc",
  "header_theme_version": "CLOSE",
  "enable_web_push": "DISABLE",
  "SESSDATA": "6711e17d%2C1749666495%2C81d55%2Ac1CjC-wKeLikWqM7uKCdDyvplm1qpaf-kBBYUCXDDeRmhX9IEfYv33BxRFjJ3QM2gYslgSVkJyQ3pmZ3U5NUY5NFplLTUzSlhfM0UwdmdVdnVVZFd5U1hlblhSdWhmMmVFS0N4V3g5anQ5SjNZUjNqbU5RZHRtRXhWR3ZQWHV1dmZmYVZUdXF1SkJ3IIEC",
  "bili_jct": "2a69873eafe208eb9737df9af42500b4",
  "DedeUserID": "206824356",
  "DedeUserID__ckMd5": "348fb3756a315342",
  "rpdid": "|(u|lk)ulu|)0J'u~JRuuRl|u",
  "CURRENT_QUALITY": "116",
  "LIVE_BUVID": "AUTO7017341856359086",
  "hit-dyn-v2": "1",
  "buvid_fp_plain": "undefined",
  "fingerprint": "16e080b0d43bf1aebc0a235260e1da4b",
  "buvid_fp": "16e080b0d43bf1aebc0a235260e1da4b",
  "bili_ticket": "eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzU1NTkzOTIsImlhdCI6MTczNTMwMDEzMiwicGx0IjotMX0.RNm-LYgtqq5suABC9eyJRPa7PfXHaYHYCQXJ3JpuAU0",
  "bili_ticket_expires": "1735559332",
  "sid": "5fat066r",
  "bsource": "search_baidu",
  "Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02": "1734692727,1735033926,1735217371,1735374878",
  "HMACCOUNT": "6AADCE7E148B2B24",
  "CURRENT_FNVAL": "4048",
  "home_feed_column": "5",
  "browser_resolution": "1738-877",
  "PVID": "8",
  "Hm_lpvt_8a6e55dbd2870f0f5bc9194cddf32a02": "1735388257",
  "bp_t_offset_206824356": "1015979152433479680",
  "b_lsid": "26B84AE7_1940D49BC1A"
}
start_headers_bls= {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
    # Requests sorts cookies= alphabetically
    # 'cookie': 'buvid3=062DC681-A603-56AD-D663-F2494BC1939759718infoc; b_nut=1724929559; _uuid=1104E4102A-EB46-68103-107510-6B61AF8101F9359177infoc; enable_web_push=DISABLE; buvid4=C262E3AC-5FEE-7775-CE36-DDE8A48B283361537-024082911-RkFkJB4egt9e+J8I/1TnCCjQ0dueP87kXuymh6k2Ka/2ycTQfkc5onYg6/WJw8Y0; header_theme_version=CLOSE; DedeUserID=3494380113496163; DedeUserID__ckMd5=27c70b7a2577e1c3; LIVE_BUVID=AUTO7917249295984859; CURRENT_FNVAL=4048; fingerprint=a29b4811d4fc77c092a0357b2b65723c; bp_t_offset_398180354=972769200160374784; buvid_fp=dfef00f0d5a0810d53566a56fe55c537; hit-dyn-v2=1; rpdid=|(Jlkllmukm|0J\'u~klYmllkY; CURRENT_QUALITY=80; bp_t_offset_3494380113496163=976475147051466752; home_feed_column=5; PVID=1; browser_resolution=1528-712; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjcxNzQ1MTAsImlhdkxNTI1MCwicGx0IjotMX0.wl5i7sguGde-c6WP2yj5zYBqIV-17UFfAr9QvsJIXEQ; bili_ticket_expires=1727174450; SESSDATA=220d938b%2C1742467311%2C26797%2A92CjD1zGQVX6vqELROuUchdtE4Vu0Rklgn3geW5zpdC03kj-JqghEzvX3qkU2iJ-_NOWMSVjBucVdhcTdvd1NWxOZDBBdHpoRGNyRVNsVktQWVdkVy1nV201VllJRFU5OE1wNVNKQlZJZktDQkdpbG9BU1B0QUxFVUR2bXhGZ3BxRlNBOXV3IIEC; bili_jct=efe74a9510deb4edb76fae5aaa2fcb8d; sid=7cflgog3; Hm_lvt_8a6e55dbd287194cddf32a02=1725956184,1726059212,1726122654,1726915330; b_lsid=D25B96BF_1921C754C50',
    'origin': 'https://link.bilibili.com',
    'priority': 'u=1, i',
    'referer': 'https://link.bilibili.com/p/center/index',
    'sec-ch-ua': '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0',
}

start_data_bls = {
    'room_id': '9533773',  #填自己的room_id
    'platform': 'pc',
    'area_v2': '624',
    'backup_stream': '0',
    'csrf_token': '2a69873eafe208eb9737df9af42500b4',#填csrf
    'csrf': '2a69873eafe208eb9737df9af42500b4',#填csrf，这两个值一样的
}


# if len(sys.argv) == 2:
#     a = sys.argv[1]
#     if a == "open":
#         response = requests.post('https://api.live.bilibili.com/room/v1/Room/startLive', cookies=start_cookies_bls, headers=start_headers_bls, data=start_data_bls).json()
#         print(response['data']['rtmp'])
# else:
#     response = requests.post('https://api.live.bilibili.com/room/v1/Room/stopLive', cookies=stop_cookies_bls, headers=stop_headers_bls, data=start_data_bls).json()
#     print(response)#注释掉的是关播的接口

def openlive(props, prop):
    response = requests.post('https://api.live.bilibili.com/room/v1/Room/startLive', cookies=start_cookies_bls, headers=start_headers_bls, data=start_data_bls).json()
    print(response['data']['rtmp'])


def offlive(props, prop):
    response = requests.post('https://api.live.bilibili.com/room/v1/Room/stopLive', cookies=start_cookies_bls, headers=start_headers_bls, data=start_data_bls).json()
    print(response)#注释掉的是关播的接口

def script_properties():
    props = obs.obs_properties_create()
    obs.obs_properties_add_button(props, "button", "开播", openlive)
    obs.obs_properties_add_button(props, "button2", "关播", offlive)
    return props